package com.sanyang.pda.ui.Issuedwork;

import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanyang.pda.bean.BindBgvinfoBean;
import com.sanyang.pda.bean.LssuedStorageBean;
import com.sanyang.pda.bean.SelectBean;
import com.sanyang.pda.bean.request.PdaCreateTaskRequest;
import com.sanyang.pda.contract.LssuedWorkContract;
import com.sanyang.pda.dialog.ConfrimDialog;
import com.sanyang.pda.event.RefreshBean;
import com.sanyang.pda.presenter.LssuedWorkPresenter;
import com.sanyang.pda.utils.PopUtisl;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.LSSUEDWORKACTIVITY)
public class LssuedWorkActivity extends BaseMvpActivity<LssuedWorkPresenter> implements LssuedWorkContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.tv_empty_pallet)
    TextView tvEmptyPallet;
    @BindView(R.id.tv_goodsshelves)
    TextView tvGoodsshelves;
    @BindView(R.id.tv_waitout)
    TextView tvWaitout;
    @BindView(R.id.tv_complete)
    TextView tvComplete;
    @BindView(R.id.tv_lssue)
    TextView tvLssue;
    @BindView(R.id.tv1Set)
    TextView tv1Set;
    @BindView(R.id.ll_bt)
    LinearLayout llBt;

    @Override
    public LssuedWorkPresenter getPresenter() {
        return new LssuedWorkPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_lssuework;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        String androidId = "" + Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        presenter.getPadBindBgvinfoByPdaSn(androidId);
    }
    @Subscribe
    public void onEvent(RefreshBean refreshBean) {
        String androidId = "" + Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        presenter.getPadBindBgvinfoByPdaSn(androidId);
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }


    @OnClick({R.id.iv_back, R.id.tv_empty_pallet, R.id.tv_goodsshelves, R.id.tv_waitout, R.id.tv_complete, R.id.tv_lssue})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
            case R.id.tv_empty_pallet:
                presenter.getStorageLocationsQuickList("1");
                break;
            case R.id.tv_goodsshelves:
                presenter.getStorageLocationsQuickList("2");
                break;
            case R.id.tv_waitout:
                presenter.getStorageLocationsQuickList("0");
                break;
            case R.id.tv_complete:
                if (bindBgvinfoBean != null){
                    ConfrimDialog confrimDialog=new ConfrimDialog(mContext, new ConfrimDialog.Onclick() {
                        @Override
                        public void onConfirm() {
                            presenter.getContinueTask(bindBgvinfoBean.getPdasn(),bindBgvinfoBean.getAgvKey());
                        }
                    });
                    confrimDialog.show("确定要任务完成吗");
                }
                break;
            case R.id.tv_lssue:
                if (TextUtils.isEmpty(palletId)){
                    showMessage("请选择空托盘区");
                    return;
                }
                if (TextUtils.isEmpty(goodsId)){
                    showMessage("请选择货架区");
                    return;
                }
                if (TextUtils.isEmpty(waitId)){
                    showMessage("请选择待出库区");
                    return;
                }
                ConfrimDialog confrimDialog=new ConfrimDialog(mContext, new ConfrimDialog.Onclick() {
                    @Override
                    public void onConfirm() {
                        PdaCreateTaskRequest request=new PdaCreateTaskRequest();
                        request.setAgvKey(bindBgvinfoBean.getAgvKey());
                        request.setPdaSN(request.getPdaSN());
                        List<String> strings=new ArrayList<>();
                        strings.add(palletId);
                        strings.add(goodsId);
                        strings.add(waitId);
                        request.setSlIdList(strings);
                        presenter.ebsyPdaCreateTask(request);
                    }
                });
                confrimDialog.show("确定要下发吗？");

                break;
        }
    }

    private String palletId, goodsId, waitId;

    @Override
    public void setList(List<LssuedStorageBean> list, String code) {
        List<SelectBean> selectBeans = new ArrayList<>();
        for (LssuedStorageBean storageBean : list) {
            if (storageBean.getSlState() != 2) {
                selectBeans.add(new SelectBean(storageBean.getSlName(), String.valueOf(storageBean.getRowGuid()),storageBean.getSlsTateStr()));
            }
        }
        PopUtisl.showSelectPop(LssuedWorkActivity.this, selectBeans, tvEmptyPallet, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                if (code.equals("1")) {
                    tvEmptyPallet.setText(bean.getName());
                    palletId = bean.getId();
                } else if (code.equals("2")) {
                    tvGoodsshelves.setText(bean.getName());
                    goodsId = bean.getId();
                } else {
                    tvWaitout.setText(bean.getName());
                    waitId = bean.getId();
                }
            }
        }, "请选择");
    }

    BindBgvinfoBean bindBgvinfoBean;

    @Override
    public void setBindBean(BindBgvinfoBean bean) {
        tvTitle.setText(bean.getAgvName());
        bindBgvinfoBean = bean;
        llBt.setVisibility(View.VISIBLE);
    }

    @Override
    public void nobBind() {
        tvTitle.setText("未绑定AGV");
        bindBgvinfoBean = null;
        llBt.setVisibility(View.GONE);
    }

  /*  @OnClick(R.id.tv1Set)
    public void onViewClicked() {
        startIntent(AppActivityKey.LSSUEDSETACTIVITY);
    }*/

}
